//
//  ZFProfessionInfoViewController.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/4/5.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFProfessionInfoViewController.h"
#import "ZFCityPickerView.h"
#import "ZHDatePickerView.h"
#import "ZFHeader.h"

@interface ZFProfessionInfoViewController ()<ZHDatePickerViewDelegate,UITextViewDelegate>

{
    NSArray *TFArray;
    NSArray *chooseButtonArray;
    
    NSString *startTime;//进入页面时间
}
@property (nonatomic,strong) ZHDatePickerView *dataPicker;
@property (nonatomic,strong) ZFCityPickerView *cityPickerV;
@property (nonatomic,strong) NSString *geocoding;//地理编码

@property (nonatomic,strong) AnimatingView *animation;
@property (nonatomic,strong) AlertBoxView *alertV;

@end

@implementation ZFProfessionInfoViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"professionInfo"];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"professionInfo"];
}
- (void)submitStatu:(ReturnStatuBlock)block
{
    self.submitStatuBlock = block;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"职业信息";
    [self initData];
}

- (void)initData
{
    _detailAdress.text = @"请输入您的详细地址";
    
    startTime = [ZFPulicAPI getCurrentTimestamp];
    
    _animation = [[AnimatingView alloc] init];
    
    _alertV = [[AlertBoxView alloc] init];
    [self.view addSubview:_alertV];

    _dataPicker = [[ZHDatePickerView alloc] initDatePickerWithDefaultDate:nil andDatePickerMode:UIDatePickerModeDate];
    _dataPicker.delegate = self;
    
    _cityPickerV = [[ZFCityPickerView alloc] init];
    _cityPickerV.dataSource = @[@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1"];
    
    TFArray = @[_company_nameTF,_telephoneTF,_job_titleTF];
    chooseButtonArray = @[_hire_dateButton,_ad_pathButton];
    
    //展示数据
    if (_professionInfoModal) {
        
        _geocoding = _professionInfoModal.ad_code;
        [self showData];
    }
}
#pragma mark —— show data
- (void)showData
{
    _company_nameTF.text = _professionInfoModal.company_name;
    _telephoneTF.text = _professionInfoModal.telephone;
    _job_titleTF.text = _professionInfoModal.job_title;
    [_hire_dateButton setTitle:_professionInfoModal.hire_date forState:UIControlStateNormal];
    [_ad_pathButton setTitle:_professionInfoModal.ad_path forState:UIControlStateNormal];
    _detailAdress.text = _professionInfoModal.detail_address;
}
#pragma mark ———— 时间选择器delegate
- (void)pickerView:(ZHDatePickerView *)pickerView didSelectDateString:(NSString *)dateString
{
    [_hire_dateButton setTitle:dateString forState:UIControlStateNormal];
}
#pragma mark ———— textview delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"请输入您的详细地址"])
    {
        textView.text = @"";
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length < 1)
    {
        textView.text = @"请输入您的详细地址";
    }
}

#pragma mark ———— button的点击事件
//选择入职时间
- (IBAction)selectDate:(id)sender {
    
    UIButton *button = (UIButton *)sender;

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [_dataPicker show];
}
//选择单位地址
- (IBAction)selectAdress:(id)sender {
    
    UIButton *button = (UIButton *)sender;
    _cityPickerV.valueDidSelect = ^(NSString *locationStr, NSString *geocoding) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:locationStr forState:UIControlStateNormal];
        _geocoding = geocoding;
    };
    [_cityPickerV show];
}

//提交信息
- (IBAction)submitProfessionInfo:(id)sender {
    
    if ([self judgeSubimtInfo]) {
        
        ZFUserProfessionInfoModal *userProfessionModal = [ZFUserProfessionInfoModal new];
        userProfessionModal.company_name = [ZFPulicAPI replaceBlank:_company_nameTF.text];
        userProfessionModal.telephone = [ZFPulicAPI replaceBlank:_telephoneTF.text];
        userProfessionModal.job_title = [ZFPulicAPI replaceBlank:_job_titleTF.text];
        userProfessionModal.hire_date = _hire_dateButton.titleLabel.text;
        userProfessionModal.ad_path = _ad_pathButton.titleLabel.text;
        userProfessionModal.ad_code = _geocoding;
        userProfessionModal.detail_address = _detailAdress.text;
        userProfessionModal.startTime = startTime;
        
        [_animation startTheAnimation];
        [ZFApplyInfoHttpRequest requestSubmitProfessionInfo:userProfessionModal sucessBlock:^(NSDictionary *dict) {
            
            [_animation stopTheAnimation];
            if ([dict[@"is_alert"] integerValue] == 1) {
                
                [_alertV appearAndDisappearAlertWithString:dict[@"msg"]];
            }
            if ([dict[@"code"] integerValue] == 1) {
                
                self.submitStatuBlock(1);
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                self.submitStatuBlock(0);
            }
        } failureBlock:^(NSInteger failSign) {
            
            [_animation stopTheAnimation];
            [_alertV appearAndDisappearAlertWithString:@"网络连接失败"];
        }];
    }
}
#pragma mark ———— 判断提交数据
- (BOOL)judgeSubimtInfo
{
    for (UITextField *TF in TFArray) {
        
        if ([ZFPulicAPI replaceBlank:TF.text].length <= 0) {
            
            [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
            return NO;
        }
    }
    for (UIButton *button in chooseButtonArray) {
        
        if ([button.titleLabel.text rangeOfString:@"请选择"].location != NSNotFound) {
            
            [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
            return NO;
        }
    }
    if ([ZFPulicAPI replaceBlank:_detailAdress.text].length <= 0 || [_detailAdress.text isEqualToString:@"请输入您的详细地址"]) {
        
        [_alertV appearAndDisappearAlertWithString:@"信息不完善" ];
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
